package com.sat.goods.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sat.common.entity.Result;
import com.sat.goods.model.Brand;
import com.sat.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 查询所有品牌数据
     *
     * @return
     */
    @GetMapping
    public Result findAll() {
        List<Brand> list = brandService.list();
        return Result.OK(list);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Brand byId = brandService.getById(id);
        return Result.OK(byId);
    }

    /**
     * 添加
     *
     * @param brand
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Brand brand) {
        boolean save = brandService.save(brand);
        if (save) {
            return Result.OK();
        } else {
            return Result.error("添加失败");
        }
    }

    /**
     * 修改
     *
     * @param brand
     * @return
     */
    @PutMapping
    public Result updata(@RequestBody Brand brand) {
        boolean b = brandService.updateById(brand);
        if (b) {
            return Result.OK();
        } else {
            return Result.error("修改失败");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        boolean b = brandService.removeById(id);
        if (b) {
            return Result.OK();
        } else {
            return Result.error("修改失败");
        }
    }

    /**
     * 条件查询
     *
     * @param brand
     * @return
     */
    @PostMapping("/search")
    public Result search(@RequestBody Brand brand) {
        List<Brand> brands = brandService.search(brand);
        return Result.OK(brands);
    }

    /**
     * 按页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result searchPage(@PathVariable Integer page, @PathVariable Integer size) {
        Page page1 = brandService.searchPage(page, size);
        return Result.OK(page1);
    }

    @PostMapping("/findPage/{page}/{size}")
    public Result findPage(@RequestBody Brand brand, @PathVariable Integer page, @PathVariable Integer size) {
        Page page1 = brandService.findPage(page, size, brand);
        return Result.OK(page1);
    }
}
